package juc.c_000;/*
   Talk is cheap. Show me the code.
 */

/**
 * @Description
 * @Date 2021/6/6
 * @Author liudl
 */
public class T03_Sleep_Yield_Join {
    public static void main(String[] args) {
//        testSleep();
//        testYield();
        testJoin();
    }

    private static void testJoin() {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println("A" + i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread t2 = new Thread(() -> {
            try {
                t1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

    }

    private static void testYield() {
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                System.out.println("A" + i);
                if (i%10==0) {
                    Thread.yield();
                }
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                System.out.println("B" + i);
                if (i%10==0) {
                    Thread.yield();
                }
            }
        }).start();
    }

    private static void testSleep() {
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    System.out.println("A" + i);
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
